package com.linkgie.galaxyframework.httpproxy.core;

import java.util.LinkedList;
import java.util.List;

import org.springframework.util.StringUtils;

import com.linkgie.galaxyframework.httpproxy.HttpRequestDefinition.ListParam;
import com.linkgie.galaxyframework.httpproxy.PropertiesConverter;
import com.linkgie.galaxyframework.httpproxy.annotations.RequestParamList;

/**
 * 请求参数表定义；
 * <p>
 * 对应于标注 {@link RequestParamList} 的属性；
 * 
 * @author huanghaiquan
 *
 */
class RequestParamListAnnotation {

	private String prefix;

	private boolean required;

	private PropertiesConverter converter;

	public RequestParamListAnnotation(String prefix, String seperator, boolean required, PropertiesConverter converter) {
		if (prefix == null || prefix.length() == 0) {
			this.prefix = "";
		} else {
			this.prefix = prefix + seperator;
		}
		this.required = required;
		this.converter = converter;
	}

	public boolean isRequired() {
		return required;
	}

	public PropertiesConverter getConverter() {
		return converter;
	}

	public String getPrefix() {
		return prefix;
	}

	public static List<ParamDefinition<RequestParamListAnnotation>> resolveParamMapDefinitions(
			List<ParamDefinition<ListParam>> reqParamAnnos) {
		List<ParamDefinition<RequestParamListAnnotation>> reqDefs = new LinkedList<ParamDefinition<RequestParamListAnnotation>>();
		for (ParamDefinition<ListParam> entry : reqParamAnnos) {
			ListParam reqParamAnno = entry.getAnnotation();
			String prefix = StringUtils.trimWhitespace(reqParamAnno.getPrefix());
			String seperator = StringUtils.trimWhitespace(reqParamAnno.getSeperator());

			Class<?> converterClazz = reqParamAnno.getConverter();
			PropertiesConverter converter = PropertiesConverterFactory.instantiatePropertiesConverter(converterClazz,
					entry.getArgumentType());
			RequestParamListAnnotation reqDef = new RequestParamListAnnotation(prefix, seperator, reqParamAnno.isRequired(),
					converter);
			reqDefs.add(new ParamDefinition<RequestParamListAnnotation>(entry.getArgumentIndex(),
					entry.getArgumentType(), reqDef));
		}
		return reqDefs;
	}

}
